// pages/my/my.js
const request = require('../../../utils/request.js');  
const { addZero,$await } = require('../../../utils/util');  // 格式化时间
import {commonAPI} from '../../../api/common/index'
const seeFm = require('../../../config.js')
const util = require('../../../utils/util.js');  
let app = getApp();
Component({
    /**
     * 页面的初始数据
     */
    data: {
        imgUrl : seeFm.imgTestUrl + '/',
        scrollHeight:'',
        radioInfo:{
        isCollection:2,
        },
        loading: false,                         // 是否正在加载中
        loaded: false,                          // 是否加载完全部数据
        page:1,
        tableData:[],
        
    },
    properties:{
        channel_number:{
            type:String,
            value:''
        },
        programe_number:{
            type:String,
            value:''
        }
    },
    methods:{
        todetail: function(e){
            const { id } = e.currentTarget.dataset;
            wx.navigateTo({
              url: `/pages/shortVideo/index?number=${id}`
            })
        },
        // 获取数据
        async getDate(){
          let params = {
            'channel_number':this.data.channel_number,
            'programe_number':this.data.programe_number,
            'page':this.data.page
          }
          const [err,res] = await $await(commonAPI.videoList({...params}))
          if(res){
            console.log(res);
            if(this.data.page == '1' && res.programme_list.length == '0'){
              if(this.data.page == 1){
                this.setData({
                  tableData:res.recommend_list
                })
              }else{
                this.setData({
                  tableData:this.data.tableData.concat(res.recommend_list)
                })
              }
              this.setData({
                page:Number(this.data.page)+1
              })
              
            }else{
              if(this.data.page == 1){
                this.setData({
                  tableData:res.programme_list
                })
              }else{
                this.setData({
                  tableData:this.data.tableData.concat(res.programme_list)
                })
              }
              this.setData({
                page:Number(this.data.page)+1
              })
              if(res.programme_list.length == '0'){
                console.log('111111')
              }
            }
            for(let i=0;i<this.data.tableData.length;i++){
              this.setData({
                ['tableData['+i+'].comment']:util.toFix(Number(this.data.tableData[i].comment),10000,'w')
              })
              this.setData({
                ['tableData['+i+'].star']:util.toFix(Number(this.data.tableData[i].star),10000,'w')
              })
              this.setData({
                ['tableData['+i+'].duration']:this.formatSeconds(Number(this.data.tableData[i].duration))
              })
            }
          }
        },
        // 格式化视频时长  格式 00:00
        formatSeconds(value) {
            if (value == undefined) {
            value = 0;
            }
            var second = parseInt(value); // 秒
            var min = 0; // 分
            var hour = 0; // 小时
            if (second > 60) {
            min = parseInt(second / 60);
            second = parseInt(second % 60);
            if (min > 60) {
                hour = parseInt(min / 60);
                min = parseInt(min % 60);
            }
            }
            
            var result = "";
            // if (hour > 0) {
            //   hour = addZero(parseInt(hour));
            //   result = hour + ":";
            // }
            if (min > 0) {
            min = addZero(parseInt(min));
            result = min + ":";
            }else{
            result = "00:";
            }
            if(second > 0){
            second = addZero(parseInt(second));
            result = result + second;
            }else{
            result = result + '00';
            }
            return result;
        },
        /**
         * 页面相关事件处理函数--监听用户下拉动作
         */
        scrolltoupper: function () {
            // 标题栏显示刷新图标，转圈圈
            wx.showNavigationBarLoading()
            // 请求最新数据
            this.setData({
                page:1
            })
            this.getDate();
            setTimeout(() => {
                // 标题栏隐藏刷新转圈圈图标
                wx.hideNavigationBarLoading()
            }, 1000);
        },
        /**
         * 页面上拉触底事件的处理函数
         */
        scrolltolower: function () {
            setTimeout(() => {
                this.getDate();
            }, 300);
        },
    },
    // 组件生命周期
    lifetimes: {
        // 在组件实例进入页面节点树时执行
        created() {
           
        },
        attached() {
            let query = this.createSelectorQuery().in(this)
            query.select('.container').boundingClientRect((rect)=>{
                let scrollHeight = (Number(rect.height.toFixed(0)))+'px'
                this.setData({scrollHeight})
            }).exec()
            this.getDate();
        },
        detached() {
            // 在组件实例被从页面节点树移除时执行
        },
    },
    
})